package org.txlcn.demo.servicea;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.txlcn.demo.common.entity.Order;
import org.txlcn.demo.common.spring.ServiceBClient;
import org.txlcn.demo.common.spring.ServiceCClient;

import java.util.UUID;

/**
 * Description:
 * Date: 2018/12/25
 *
 * @author ujued
 */
@Service
@Slf4j
public class OrderServiceImpl {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ServiceBClient serviceBClient;
    @Autowired
    private ServiceCClient serviceCClient;
    @Autowired
    private RestTemplate restTemplate;


    @LcnTransaction
    @Transactional(rollbackFor = Exception.class)
    public String execute(Integer money, Integer number) {
        Order order = new Order();
        order.setId(UUID.randomUUID().toString());
        order.setNumber(1);
        orderMapper.insert(order);
        //ribbon调用
        String dResp = restTemplate.getForObject("http://127.0.0.1:12002/rpc?money=" + money, String.class);

        // feign调用
        String eResp = serviceCClient.rpc(number);

        // step3. execute local transaction
//        int i = 1 / 0;
        return "ok-service-a";
    }
}
